/**
 * 点击其他位置进行操作（如关闭弹窗）
 */
export const clickOtherPosToClose = (
    targetEl: HTMLElement[] | HTMLElement | NodeList,
    callback: () => void
) => {
    const listener = (e: Event) => {
        const isNodeList = targetEl.constructor.name === 'NodeList'
        if (Array.isArray(targetEl) || isNodeList) {
            const contains = (isNodeList ? Array.from(targetEl as NodeList) : targetEl as HTMLElement[]).find(
                (boxEl) => boxEl!.contains(e.target as HTMLElement)
            )
            if (!contains) {
                callback()
                document.removeEventListener('click', listener, true)
            }
        } else if (!(targetEl as HTMLElement).contains(e.target as HTMLElement)) {
            callback()
            document.removeEventListener('click', listener, true)
        }
    }
    document.addEventListener('click', listener, true)
}